package org.ss.psci.dao;

import java.util.List;
import java.util.Set;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.ss.psci.entity.PmAddressList;
import org.ss.psci.entity.PmMessage;

/**
 * Data access object (DAO) for domain model class PmMessage.
 * 
 * @see org.ss.psci.entity.PmMessage
 * @author MyEclipse Persistence Tools
 */

public class PmMessageDAO extends HibernateDaoSupport {
	private static final Log log = LogFactory.getLog(PmMessageDAO.class);
	// property constants
	public static final String USERNAME = "username";
	public static final String TITLE = "title";
	public static final String CONTENT = "content";
	public static final String REM_DATE = "remDate";
	public static final String CT_TIME = "ctTime";
	public static final String PHONE = "phone";
	public static final String MAIL = "mail";
	public static final String SENDER = "sender";

	
	public PmMessage copyEntity(PmMessage dest, PmMessage orig) {
		if (dest == null || orig == null) {
			return new PmMessage();
		}
		if (orig.getTitle() != null) {
			dest.setTitle(orig.getTitle());
		}
		if (orig.getUsername() != null) {
			dest.setUsername(orig.getUsername());
		}
		if (orig.getContent() != null) {
			dest.setContent(orig.getContent());
		}
		if (orig.getMail() != null) {
			dest.setMail(orig.getMail());
		}
		if (orig.getRemDate()!= null) {
			dest.setRemDate(orig.getRemDate());
		}
		if (orig.getSender()!= null) {
			dest.setSender(orig.getSender());
		}
		if (orig.getPhone() != null) {
			dest.setPhone(orig.getPhone());
		}
		return dest;
	}
	protected void initDao() {
		// do nothing
	}

	public void save(PmMessage transientInstance) {
		log.debug("saving PmMessage instance");
		try {
			getHibernateTemplate().saveOrUpdate(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(PmMessage persistentInstance) {
		log.debug("deleting PmMessage instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public PmMessage findById(java.lang.Integer id) {
		log.debug("getting PmMessage instance with id: " + id);
		try {
			PmMessage instance = (PmMessage) getHibernateTemplate().get(
					"org.ss.psci.entity.PmMessage", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(PmMessage instance) {
		log.debug("finding PmMessage instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding PmMessage instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from PmMessage as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}
	public List findByUsernameAndTitle(String username, String title) {

		try {
			String queryString = "from PmMessage as model where model.username=? and model.title=?";
			return getHibernateTemplate().find(queryString, new Object[]{username,title});
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}
	public List findByUsername(Object username) {
		return findByProperty(USERNAME, username);
	}

	public List findByTitle(Object title) {
		return findByProperty(TITLE, title);
	}

	public List findByContent(Object content) {
		return findByProperty(CONTENT, content);
	}

	public List findByRemDate(Object remDate) {
		return findByProperty(REM_DATE, remDate);
	}

	public List findByCtTime(Object ctTime) {
		return findByProperty(CT_TIME, ctTime);
	}

	public List findByPhone(Object phone) {
		return findByProperty(PHONE, phone);
	}

	public List findByMail(Object mail) {
		return findByProperty(MAIL, mail);
	}

	public List findBySender(Object sender) {
		return findByProperty(SENDER, sender);
	}

	public List findAll() {
		log.debug("finding all PmMessage instances");
		try {
			String queryString = "from PmMessage";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public PmMessage merge(PmMessage detachedInstance) {
		log.debug("merging PmMessage instance");
		try {
			PmMessage result = (PmMessage) getHibernateTemplate().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(PmMessage instance) {
		log.debug("attaching dirty PmMessage instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(PmMessage instance) {
		log.debug("attaching clean PmMessage instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static PmMessageDAO getFromApplicationContext(ApplicationContext ctx) {
		return (PmMessageDAO) ctx.getBean("PmMessageDAO");
	}
}